﻿#include "AddType.h"
void openJsonFile(QByteArray &jsonData,
                  QJsonParseError &error,
                  QJsonDocument &doc,
                  const char* JsonPath)
{
    QFile file(JsonPath);
    if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "AddType::openJsonFileJson ---> open file -name=" << JsonPath << "success.";
        while (!file.atEnd()) {
            QByteArray line = file.readLine().trimmed();
            if (!line.isEmpty()) {
                jsonData += line;
            }
        }
        doc = QJsonDocument::fromJson(jsonData, &error);
        file.close();
    }else{
        qDebug() << "AddType::openJsonFileJson ---> open file -name=" << JsonPath << "failed.";
        return;
    }
}

void openxmlFile(const char *XmlPath, QString &xmlcontent)
{
    QFile file(XmlPath);
    if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "ParsexmlIns::openxml ---> open xml file success.";
        qDebug() << XmlPath;
        xmlcontent = file.readAll();

    }else{
        qDebug() << "ParsexmlIns::openxml ---> open xml file failed.";
    }
    file.close();
}

void openxmlFile(QString &XmlPath, QString &newxmlcontent)//jc
{
    QFile file(XmlPath);
    if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)){
        qDebug() << "ParsexmlIns::openxml ---> open xml file success.";
        qDebug() << XmlPath;
//        qDebug() << newxmlcontent;
        QTextStream out(&file);
//        qDebug() << newxmlcontent.toLocal8Bit().constData();
        out << newxmlcontent;
        file.close();
    }else{
        qDebug() << "ParsexmlIns::openxml ---> open xml file failed.";
    }
    file.close();
}

void openxmlFile(QString &XmlPath, const QString &alpname, QString &newxmlcontent)
{
    XmlPath += alpname;
    QFile file(XmlPath);
    if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate | QIODevice::NewOnly)){
        qDebug() << "ParsexmlIns::openxml ---> open xml file success.";
        qDebug() << XmlPath;
        //        qDebug() << newxmlcontent;
        QTextStream out(&file);
        //        qDebug() << newxmlcontent.toLocal8Bit().constData();
        out << newxmlcontent;
        file.close();
    }
}
